﻿
@{
    Layout = null;
}


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/layui/css/layui.css" rel="stylesheet" />
    <link href="~/layuimini/css/public.css" rel="stylesheet" />
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">


            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>角色选择</legend>
            </fieldset>

            <form class="layui-form" action="" lay-filter="example">
                <input type="hidden" name="RoleID" value="@ViewBag.RoleID" disabled>
                <input type="hidden" name="Role" value="@ViewBag.Role" disabled>
                
                <div class="layui-form-item" pane="">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        @Html.Raw(ViewBag.RoleList)
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script src="~/layui/layui.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script>
        layui.use(['form', 'layedit', 'laydate', 'jquery'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate
                , $ = layui.jquery

            //页面加载的时候就初始几个值
            initValue();
 
            //监听复选框
            form.on('checkbox(role)', function (data) {
                var number = $('input[name="Role"]')
                var value = data.value
                var array = [];
                if(number.val()!='')
                {
                    array = number.val().split(",");
                }


                if (data.elem.checked) {
                    array.push(value)
                    number.val(array.sort().join(","));
                } else {
                    var arrtmp = [];
                    for (var i = 0; i < array.length; i++) {
                        if (array[i]!=value) {
                            arrtmp.push(array[i]);
                        }
                    }
                    number.val(arrtmp.sort().join(","));
                }
            });
 
            /**
             * input 框初始 赋值 到checkedbox上
             * author lengff
             */
            function initValue() {
                var param = $("input[name='Role']").val()
                var checkBoxs = $("input[type='checkbox']")
                var array = param.split(",");
                for (var i = 0; i < array.length; i++) {
                    for (var j = 0; j < checkBoxs.length; j++) {
                        var checkbox = $(checkBoxs[j]);
                        if (checkbox.val() == array[i]) {
                            checkbox.attr('checked','checked');
                            break;
                        }
                    }
                }
                form.render('checkbox');
            }


            //监听提交
            form.on('submit(demo1)', function (data) {
                //layer.msg(JSON.stringify(data.field));
                //return false;


                $.ajax({
                    url: '@Url.Action("RoleSelect2Async")',
                    type: 'post',
                    dataType: 'json',
                    data: data.field,
                    success: function (data) {
                        console.log(data);
                        //Info,Success,Warning,Error
                        var type = data.Type;
                        if (type == 1) {
                            layer.msg(data.Content, { icon: 6, time: 2000 },
                                function () {
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                });

                        } else {
                            layer.alert(data.Content, { icon: 5 });
                        }
                    }
                });
                //layer.alert(JSON.stringify(data.field), {
                //    title: '最终的提交信息'
                //})
                return false;
            });

            //表单初始赋值
            @*form.val('example', {
                "ID": "@ViewBag.model.ID"
                    , "Code": "@ViewBag.model.Code"
                    , "Name": "@ViewBag.model.Name"
                    , "Address": "@ViewBag.model.Address"
                    , "LinkMan": "@ViewBag.model.LinkMan"
                    , "Tel": "@ViewBag.model.Tel"
                    , "Remark": "@ViewBag.model.Remark"
            })*@


        });
    </script>

</body>
</html>